const request = require("../../../utils/request.js");
const CountryCode = require("../../../utils/countryCode.js")
const util = require("../../../utils/util.js")
const app = getApp()
Page({
  data: {
    storeInfo: [],
    countryName: "",
    countryImg: "",
    shopDescImgs: [],
    collectionArr: [],
    isSaved: false,
    storeId: -1,
    storeColId: -1,
    /**测试数据 */
    banners: [{
        bannerId: '01',
        bannerImgUrl: 'http://seopic.699pic.com/photo/50047/8229.jpg_wh1200.jpg'
      },
      {
        bannerId: '02',
        bannerImgUrl: 'http://seopic.699pic.com/photo/50047/8229.jpg_wh1200.jpg'
      },
      {
        bannerId: '03',
        bannerImgUrl: 'http://seopic.699pic.com/photo/50047/8229.jpg_wh1200.jpg'
      }
    ],
    storeId: -1
  },
  onLoad: function(option) {
    var that = this;
    var storeId = option.storeId;
    that.setData({
      storeId: storeId
    })
    console.log("onLoad: " + storeId);
    that.setData({
      storeId: storeId
    });
  },
  onShow: function() {
    var that = this
    var storeId = that.data.storeId;
    console.log("onSHow: " + storeId);
    var url = request.URL + "/store";
    var countryCode = CountryCode.countryCode;
    request.request({
      url: url,
      data: {
        userId: 1,
        storeId: storeId
      },
      success: function(res) {
        var arr = res.data.store;
        var collectionArr = res.data.storeCollection;
        var storeColId = -1;
        console.log(res);
        console.log(collectionArr);
        wx.setNavigationBarTitle({
          title: arr.storeName,
        })
        var isSaved = false;
        for (var i = 0; i < collectionArr.length; i++) {
          var collectedStoreId = collectionArr[i].storeId;
          if (collectedStoreId == arr.storeId) {
            isSaved = true;
            storeColId = collectionArr[i].storeColId;
          }
        }
        var countryName = "";
        var countryImgUrl = "";
        var countryInfo = CountryCode.getCountryInfo(arr.countryId) || null;
        if (countryInfo == null) {
          countryName = "其它";
          countryImgUrl = countryImgUrl;
        } else {
          countryName = countryInfo.chineseName;
          countryImgUrl = countryInfo.img;
        }
        var storeDescImgsString = arr.detailImgArray;
        var storeDescImgsArray = util.string2Array(storeDescImgsString);
        that.setData({
          storeInfo: arr,
          collectionArr: collectionArr,
          isSaved: isSaved,
          countryName: countryName,
          countryImg: countryImgUrl,
          shopDescImgs: storeDescImgsArray,
          storeColId: storeColId
        })

      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**
   * 进入优惠商品页
   *
   */
  openSaleList: function(e) {
    var that = this;
    var storeId = that.data.storeId;
    var storeName = that.data.storeInfo.storeName;
    console.log(storeId)
    wx.navigateTo({
      url: '../sale_item_list/sale_item_list' + '?storeId=' + storeId + '&storeName=' + storeName
    })
  },
  /**
   * 进入店铺内商品分类页
   */
  openInShopCate: function(e) {
    var that = this;
    var storeId = that.data.storeId;
    var storeName = that.data.storeInfo.storeName;
    wx.navigateTo({
      url: './in_shop_cate/in_shop_cate' + '?storeId=' + storeId + '&storeName=' + storeName
    })
  },
  /**
   * 打开店铺全部评价页
   */
  openAllComments: function(e) {
    var that = this;
    var storeId = that.data.storeId;
    var storeName = that.data.storeInfo.storeName;
    wx.navigateTo({
      url: './all_comments/all_comments' + '?storeId=' + storeId + '&storeName=' + storeName
    })
  },
  /**跳转到搜索界面 */
  toSearchPage: function(e) {
    var that = this;
    var storeId = that.data.storeId;
    wx.navigateTo({
      url: '/pages/search/search' + '?storeId=' + storeId
    })
  },

  /**
   *  收藏店铺
   */
  saveShop: function(e) {
    var that = this;
    var url = request.URL + '/collection/store/add';
    var storeId = that.data.storeId;
    console.log(app.globalData.userInfo)
    if (app.globalData.userInfo != null) {
      //userId 测试用1
      var userId = 1;
      request.request({
        url: url,
        data: {
          userId: 1,
          storeId: storeId
        },
        success: function (res) {
          that.onShow();
          console.log(res);
        },
        fail: function () {
          request.showFailInfo()
        }
      })
      console.log(storeId);
    }
  },
  /**
   *   取消收藏店铺
   */
  unSaveShop: function(e) {
    var that = this;
    var url = request.URL + '/collection/store/delete';
    var storeColId = that.data.storeColId;
    console.log(that.data.storeColId)
    request.request({
      url: url,
      data: {
        storeColId: storeColId
      },
      success: function(res) {
        that.onShow();
        console.log(res);
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  //转发小程序
  onShareAppMessage: function() {
    var that = this;
    var storeInfo = that.data.storeInfo;
    var storeId = that.data.storeId;
    return {
      title: storeInfo.storeName,
      path: '/pages/details/shop_details/shop_details?storeId=' + storeId,
      success: function(res) {
        //转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        })
      },
      fail: function(res) {
        //转发失败
      }
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    if (e.detail.errMsg != 'getUserInfo:ok') {
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权，将无法显示个人信息',
        showCancel: false,
        confirmText: '知道了'
      })
    } else {
      wx.login({
        success: function (res) {
          console.log(res)
          var code = res.code;
        }
      })
      app.globalData.userInfo = e.detail.userInfo
      console.log(app.globalData.userInfo)
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  },
});